import com.sun.source.tree.Tree;

public class TestBinaryTree {
    static public class TreeNode{
        public char val;        //存储二叉树节点中的数据
        public TreeNode left;   //左节点的引用
        public TreeNode right;  //右节点的引用

        public TreeNode(char val){
            this.val = val;
        }
    }
    //简单的创建二叉树
    public TreeNode createTree(){
        TreeNode A = new TreeNode('a');
        TreeNode B = new TreeNode('b');
        TreeNode C = new TreeNode('c');
        TreeNode D = new TreeNode('d');
        TreeNode E = new TreeNode('e');
        TreeNode F = new TreeNode('f');
        TreeNode G = new TreeNode('g');
        TreeNode H = new TreeNode('h');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;//返回最大的二叉树的根节点
    }

    //前序遍历  根  左  右
    void preOrder(TreeNode root){
        if(root == null){
            return;   //可以简单理解为返回上一个节点
        }

        System.out.println(root.val + " ");
        //递归遍历左子树
        preOrder(root.left);
        //递归遍历右子树
        preOrder(root.right);
    }

    //中序遍历   左  根  右
    void inOrder(TreeNode root){
        if(root == null){
            return;
        }
        //递归遍历左子树
        inOrder(root.left);

        System.out.println(root.val + " ");
        //递归遍历右子树
        inOrder(root.right);
    }

    //后序遍历   左  右  根
    void postOrder(TreeNode root){
        if(root == null){
            return;
        }
        //递归遍历左子树
        postOrder(root.left);
        //递归遍历右子树
        postOrder(root.right);

        System.out.println(root.val + " ");
    }

    //获取树中的节点个数
    public int size(TreeNode root){
         if(root == null){
             return 0;
         }
         return 1 + size(root.left) + size(root.right);
    }

    //求叶节点的个数
    public int getLeafNodeCount(TreeNode root){
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            return 1;
        }
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }

    //求第k层的节点的个数
    public int getKLevelNodeCount(TreeNode root,int k){
        if(root == null){
            return 0;
        }
        if(k == 1){
            return 1;
        }
        return getKLevelNodeCount(root.right,k - 1)
                +getKLevelNodeCount(root.left,k - 1);
    }

    //获取二叉树高度
    public int getHeight(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);

        return Math.max(leftHeight,rightHeight) + 1;
    }

    //检测值为value的节点是否存在
    public TreeNode find(TreeNode root,char val){
        if(root == null){
            return null;
        }
        if(root.val == val){
            return root;
        }

        TreeNode leftNode = find(root.left,val);
        if(leftNode != null){
            return leftNode;
        }
        TreeNode rightNode = find(root.right,val);
            if (rightNode != null) {
                return rightNode;
            }
        return null;
    }
}
